<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template  #headerSearch-btn-append="{ searchForm }">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="gotoDetail()">新增</el-button>
        <el-button type="success" size="small" icon="el-icon-download" @click="doDownload(searchForm)">导出采购合同</el-button>
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
      <template #table-handle-edit="scope">
        <el-button type="text" :icon="scope.row.status === 'save'? 'el-icon-edit': 'el-icon-view'" @click="gotoDetail(scope.row.billNo)">{{ scope.row.status === 'save'? '编辑': '查看' }}</el-button>
      </template>
      <template #table-handle-audit="scope">
        <el-button type="text" icon="el-icon-s-promotion" @click="doPromotion(scope.row)" :style="scope.row.status === 'save'? '': 'color: var(--el-color-warning)'">{{ scope.row.status === 'save'? '审核': '反审' }}</el-button>
      </template>
      <template #table-handle-delete="scope">
        <el-button v-if="scope.row.status === 'save'" type="text" icon="el-icon-delete" @click="doDelete(scope.row)" style="color: var(--el-color-danger)">删除</el-button>
        <span v-else></span>
      </template>
      <template #table-handle-push="scope">
        <el-button v-if="scope.row.status === 'audit'" type="text" icon="el-icon-download" @click="openPushForm(scope.row)" style="color: var(--el-color-success);margin-left: 10px;">下推</el-button>
        <span v-else></span>
      </template>
    </vb-page>
  </div>
  <el-dialog  v-model="showPushDialog" width="500px" center>
    <el-form :model="pushForm" label-width="80px" ref="form">
      <el-form-item label="单据类型" prop="orderType" :rules="[{ required: true, message: '请选择单据类型' } ]">
        <el-select size="small" v-model="pushForm.orderType" placeholder="请选择单据类型" clearable>
          <el-option v-for="item in orderTypeList" :key="item.key" :label="item.name" :value="item.key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input size="small" type="textarea" v-model="pushForm.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" icon="el-icon-back" @click="showPushDialog = false">取消</el-button>
        <el-button type="primary" size="small" icon="el-icon-check" @click="doPushOrder">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref } from 'vue'
const { proxy } = getCurrentInstance()

const loading = ref(false)
const showPushDialog = ref(false)
const orderTypeList = ref([])
const pushForm = ref({})

/**
 * 获取字典
 */
const getDictionary = async () => {
  loading.value = true
  try {
    let res = await proxy.$api.getChildDictionaryListByKeys({ keys: ['orderType'] })
    if (!res.res) return
    orderTypeList.value = res.orderType
  } finally {
    loading.value = false
  }
}

/**
 * 下载采购合同列表
 */
const doDownload = param => {
  loading.value = true
  try {
    return proxy.callApi('download_pur_con', param)
  } finally {
    loading.value = false
  }
}

/**
 * 审核或反审单据
 */
const doPromotion = async row => {
  loading.value = true
  try {
    if (row.status === 'save') {
      let res = await proxy.callApi('audit_pur_con', { billNo: row.billNo })
      if (!res.res) return
      proxy.$message.success(`审核完成!`)
      row.status = 'audit'
    } else {
      let res = await proxy.callApi('un_audit_pur_con', { billNo: row.billNo })
      if (!res.res) return
      proxy.$message.success(`反审完成!`)
      row.status = 'save'
    }
  } finally {
    loading.value = false
  }
}

/**
 * 删除单据
 */
const doDelete = async row => {
  try {
    await proxy.$confirm(`确定删除单据【${ row.billNo }】吗`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
  } catch(e) {
    return
  }
  loading.value = true
  try {
    let res = await proxy.callApi('delete_pur_con', { billNo: row.billNo })
    if (!res.res) return
    proxy.$message.success('已删除!')
    proxy.$refs.page.reload()
  } finally {
    loading.value = false
  }
}

/**
 * 跳转详细页面
 */
const gotoDetail = (billNo = '') => {
  let url = '/purchase/purConDetail'
  if (billNo) {
    url += `?billNo=${ billNo }`
  }
  return proxy.$router.push(url)
}

/**
 * 打开下推
 */
const openPushForm = row => {
  pushForm.value.billNo = row.billNo
  pushForm.value.remark = row.remark
  showPushDialog.value = true
}

/**
 * 下推
 */
const doPushOrder = async () => {
  return proxy.$refs.form.validate(async valid => {
    if (!valid) return;
    let param = {
      billNo: pushForm.value.billNo,
      orderType : pushForm.value.orderType,
      remark : pushForm.value.remark
    }
    loading.value = true
    try {
      let res = await proxy.callApi('push_pur_con_order', param)
      if (!res.res) return
      proxy.$message.success(`下推完成!`)
      showPushDialog.value = false
      return proxy.$router.push('/purchase/purOrderList')
    } finally {
      loading.value = false
    }
  })
}
getDictionary()
</script>

<style scoped>
</style>
